using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using SqlSugar;
using WebServer.Config;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
    //设置版本标题
    options.SwaggerDoc("v1",new OpenApiInfo {Title = "维修工单看板API", Version = "v1"  });
    //设置对象类型参数默认值
    options.SchemaFilter<DefaultValueSchemaFilter>();
});
//替换内置IOC
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(container =>
{
    #region 通过模块化的方法注册接口层和实现层
    container.RegisterModule(new AutofacModuleRegister());
    #endregion

    container.Register<ISqlSugarClient>(context =>
    {
        SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
        {
            DbType = DbType.SqlServer,
            IsAutoCloseConnection = true,
            ConnectionString = builder.Configuration.GetConnectionString("conn"),
            InitKeyType = InitKeyType.Attribute // 从特性中读取主键和自增列信息
        });
        return db;
    });
});
//AutoMapper映射
builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();

app.UseRouting();
app.UseCors(x => x
    .AllowAnyOrigin()
    .AllowAnyMethod()
    .AllowAnyHeader());

app.UseAuthorization();

app.MapControllers();

app.Run();